<template>
  <div class="login-container">
    <el-tabs v-model="activeTab" type="card" @tab-click="handleTabClick">
      <!-- 账号密码登录 -->
      <el-tab-pane label="账号密码登录" name="account">
        <el-form :model="accountForm" label-width="100px" class="login-form">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="accountForm.username" autocomplete="off" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="accountForm.password" autocomplete="off" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleAccountLogin">登录</el-button>
            <el-link @click="navigateToRegister" class="register-link">没有账号？注册</el-link>
          </el-form-item>
        </el-form>
      </el-tab-pane>

      <!-- 短信登录 -->
      <el-tab-pane label="短信登录" name="sms">
        <el-form :model="smsForm" label-width="100px" class="login-form">
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="smsForm.phone" autocomplete="off" />
          </el-form-item>
          <el-form-item label="验证码" prop="code">
            <el-input v-model="smsForm.code" autocomplete="off" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSmsLogin">登录</el-button>
            <el-button type="success" @click="sendSmsCode" :disabled="smsCodeSending">
              {{ smsCodeSending ? smsForm.countdown + 's' : '发送验证码' }}
            </el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>

      <!-- 邮箱登录 -->
      <el-tab-pane label="邮箱登录" name="email">
        <el-form :model="emailForm" label-width="100px" class="login-form">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="emailForm.email" autocomplete="off" />
          </el-form-item>
          <el-form-item label="验证码" prop="code">
            <el-input v-model="emailForm.code" autocomplete="off" />
          </el-form-item>
          <el-form-item class="button-group">
            <el-button type="primary" @click="handleEmailLogin">登录</el-button>
            <el-button type="success" @click="sendEmailCode" :disabled="emailCodeSending">
              {{ emailCodeSending ? emailForm.countdown + 's' : '发送验证码' }}
            </el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>

      <!-- 注册 -->
      <el-tab-pane label="注册" name="register">
        <el-form :model="registerForm" label-width="100px" class="login-form">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="registerForm.username" autocomplete="off" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="registerForm.password" autocomplete="off" />
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input type="password" v-model="registerForm.confirmPassword" autocomplete="off" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleRegister">注册</el-button>
            <el-link @click="navigateToLogin" class="register-link">已有账号？登录</el-link>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { reactive, ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import axiosInstance from '../service/baseService'; 
import { ElMessage } from 'element-plus';

export default {
  setup() {
    const router = useRouter();
    const activeTab = ref('account');

    const accountForm = reactive({
      username: '',
      password: ''
    });

    const smsForm = reactive({
      phone: '',
      code: '',
      countdown: 0,
    });

    const emailForm = reactive({
      email: '',
      code: '',
      countdown: 0,
    });

    const registerForm = reactive({
      username: '',
      password: '',
      confirmPassword: ''
    });

    const smsCodeSending = ref(false);
    const emailCodeSending = ref(false);

    const sendSmsCode = async () => {
      if (!smsCodeSending.value) {
        try {
          smsCodeSending.value = true;
          await axiosInstance.post('/auth/send-code-phone', smsForm);
          ElMessage.success('短信验证码发送成功');
          startSmsCodeCountdown();
        } catch (error) {
          ElMessage.error('短信验证码发送失败');
          smsCodeSending.value = false;
        }
      }
    };

    const sendEmailCode = async () => {
      if (!emailCodeSending.value) {
        try {
          emailCodeSending.value = true;
          await axiosInstance.post('/auth/send-code',emailForm);
          ElMessage.success('邮箱验证码发送成功');
          startEmailCodeCountdown();
        } catch (error) {
          ElMessage.error('邮箱验证码发送失败');
          emailCodeSending.value = false;
        }
      }
    };

    const startSmsCodeCountdown = () => {
      let countdown = 60;
      const timer = setInterval(() => {
        if (countdown > 0) {
          smsForm.countdown = countdown;
          countdown--;
        } else {
          clearInterval(timer);
          smsCodeSending.value = false;
          smsForm.countdown = 0;
        }
      }, 1000);
    };

    const startEmailCodeCountdown = () => {
      let countdown = 60;
      const timer = setInterval(() => {
        if (countdown > 0) {
          emailForm.countdown = countdown;
          countdown--;
        } else {
          clearInterval(timer);
          emailCodeSending.value = false;
          emailForm.countdown = 0;
        }
      }, 1000);
    };

    const handleAccountLogin = async () => {
      const loginData = {
        userName: accountForm.username,
        password: accountForm.password
      };
      try {
        await axiosInstance.post('/auth/login-with-password', loginData);
        ElMessage.success('登录成功');
        router.push('/IndexView');
      } catch (error) {
        ElMessage.error('登录失败');
      }
    };

    const handleSmsLogin = async () => {
      try {
        await axiosInstance.post('/auth/login-with-phone', smsForm);
        ElMessage.success('登录成功');
        router.push('/IndexView');
      } catch (error) {
        ElMessage.error('登录失败');
      }
    };

    const handleEmailLogin = async () => {
      try {
        await axiosInstance.post('/auth/login-with-code', emailForm);
        ElMessage.success('登录成功');
        router.push('/IndexView');
      } catch (error) {
        ElMessage.error('登录失败');
      }
    };

    const handleRegister = async () => {
      if (registerForm.password !== registerForm.confirmPassword) {
        ElMessage.error('密码和确认密码不一致');
        return;
      }
      try {
        await axiosInstance.post('/auth/register', {
          username: registerForm.username,
          password: registerForm.password
        });
        ElMessage.success('注册成功');
        activeTab.value = 'account';
      } catch (error) {
        ElMessage.error('注册失败');
      }
    };

    const navigateToRegister = () => {
      activeTab.value = 'register';
    };

    const navigateToLogin = () => {
      activeTab.value = 'account';
    };

    const handleTabClick = (tab) => {
      console.log('Tab clicked:', tab.name);
    };

    onMounted(() => {
      smsForm.countdown = 0;
      emailForm.countdown = 0;
    });

    return {
      smsCodeSending,
      emailCodeSending,
      sendSmsCode,
      sendEmailCode,
      handleAccountLogin,
      handleSmsLogin,
      handleEmailLogin,
      handleRegister,
      navigateToRegister,
      navigateToLogin,
      handleTabClick,
      activeTab,
      accountForm,
      smsForm,
      emailForm,
      registerForm,
    };
  },
};
</script>

<style scoped>
body {
  background-image: url('https://i03piccdn.sogoucdn.com/01557abf37472282');
  background-color: #f0f0f0;
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  background-attachment: fixed;
  height: 100vh;
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-container {
  max-width: 400px;
  width: 100%;
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: absolute;
  left: 40%;
  top: 25%;
}

.login-form {
  margin-top: 20px;
}

.el-tabs__nav {
  margin-bottom: 20px;
}

.el-form-item {
  margin-bottom: 15px;
}

.el-button {
  width: 100%;
  margin-top: 10px;
}

.button-group {
  display: flex;
  justify-content: space-between;
}

.register-link {
  display: block;
  text-align: center;
  margin-top: 20px;
  color: #409eff;
}

.el-input__inner {
  color: #000; /* 确保输入的文字颜色可见 */
}
</style>
